﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PracticLesson13._12._14
{
    class Program
    {
       
        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            Random rnd = new Random();
            int a = rnd.Next(0, 70);
            int b = rnd.Next(0, 20);
            int c = 0;
            int d = 0;
            ConsoleColor Red = ConsoleColor.Red;
            ConsoleColor Green = ConsoleColor.Green;
            ConsoleColor Blue = ConsoleColor.Blue;
            Robot r = new Robot(a, b, Red);
            Robot r1 = new Robot(c = rnd.Next(0, 70), d = rnd.Next(0, 20), Green);
            Robot r2 = new Robot(c = rnd.Next(0, 70), d = rnd.Next(0, 20), Blue);
            ulong[,] stepR1 = new ulong[70, 25];// координаты подвижный роботов
            ulong[,] stepR = new ulong[70, 25];// координаты неподвижной точки
            RememberStep(r, stepR);
           // Console.ForegroundColor = ConsoleColor.Red;
            r.Print();
            do
            {
                //Console.ForegroundColor = ConsoleColor.Blue;
                ChekingSteps(r1, stepR1, rnd);
                r1.Print();
                RememberStep(r1, stepR1);

                //Console.ForegroundColor = ConsoleColor.Green;
                ChekingSteps(r2, stepR1, rnd);
                r2.Print();
                RememberStep(r2, stepR1);

                Thread.Sleep(100);
            } while (stepR[a,b]!=stepR1[a,b]);
            
            Console.SetCursorPosition(a, b);
            Console.WriteLine("End Game");
            Console.ReadKey();
        }
    
        static void MovingRobot(Random rnd,Robot r)
        {
               int q = rnd.Next(1,5);
               if (q == 1)
                   r.MoveRight(1);
               if (q == 2)
                   r.MoveLeft(1);
               if (q == 3)
                   r.MoveBottom(1);
               if (q == 4)
                   r.MoveTop(1);
        }
        static void RememberStep(Robot r, ulong  [,] step)
        {
            //r._x = Math.Max(0, 71);
            //r._y = Math.Max(0, 28);
            step[r._x, r._y] = 27;
        }

        static void ChekingSteps (Robot r, ulong [,] step, Random rnd)
        {
            int x = r._x;
            int y = r._y;

            if (step[r._x, r._y] == 27)
            {
                do
                {
                    r._x = x;
                    r._y = y;
                    MovingRobot(rnd, r);

                    if (step[r._x, r._y] == 27)
                    {
                        if (r._x == 0)
                        {
                            r._x += 1;
                        }
                        if (r._y == 0)
                        {
                            r._y += 1;
                        }
                        if (r._x == 69)
                        {
                            r._x -= 1;
                        }
                        if (r._y == 24)
                        {
                            r._y -= 1;
                        }
                        int a = rnd.Next(-1,2);
                        int b = rnd.Next(-1,2);
                        step[r._x + a, r._y + b] = 1;
                    }
                                    
                    
                } while (step[r._x, r._y] == 27);
            }
            else MovingRobot(rnd, r);
            
            

        }
    }
}
